package com.efeichong.project.controller;

import com.efeichong.common.domain.ResponseData;
import com.efeichong.project.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author lxk
 * @date 2020/12/3
 * @description
 */
@Api(tags = "文件上传接口", value = "文件上传接口")
@RequiredArgsConstructor
@RequestMapping("oss")
@RestController
public class OssController {

    private final OssService ossService;

    @ApiOperation("文件上传")
    @SneakyThrows
    @PostMapping(value = "upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<String> upload(@RequestPart("file") MultipartFile file,
                                       @RequestParam(value = "folderName", required = false) String folderName,
                                       @RequestParam(value = "suffix", defaultValue = "jpg") String suffix) {
        String url = ossService.upload(file, folderName, suffix);
        return new ResponseData(url);
    }

    @ApiOperation("文件下载")
    @SneakyThrows
    @GetMapping(value = "download")
    public void download(@RequestParam(value = "url") String url,
                         HttpServletResponse response) {
        ossService.download(url, response);
    }

    @ApiOperation("文件删除")
    @SneakyThrows
    @GetMapping(value = "delete")
    public ResponseData delete(@RequestParam(value = "url") String url) {
        ossService.delete(url);
        return new ResponseData();
    }

}
